package mzj.jarfinder;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FindInJar {
	public String className;
	public ArrayList jarFiles = new ArrayList();

	public FindInJar() {
	}

	public FindInJar(String className) {
		this.className = className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List findClass(String dir, boolean recurse) {
		searchDir(dir, recurse);
		return this.jarFiles;
	}

	protected void searchDir(String dir, boolean recurse) {
		try {
			File d = new File(dir);
			if (!d.isDirectory()) {
				return;
			}
			File[] files = d.listFiles();
			for (int i = 0; i < files.length; i++)
				if ((recurse) && (files[i].isDirectory())) {
					searchDir(files[i].getAbsolutePath(), true);
				} else {
					String filename = files[i].getAbsolutePath();
					String subfilename = files[i].getName();

					if ((filename.endsWith(".jar"))
							|| (filename.endsWith(".zip"))
							|| (filename.endsWith(".JAR"))
							|| (filename.endsWith(".ZIP"))) {
						ZipFile zip = new ZipFile(filename);
						Enumeration entries = zip.entries();
						while (entries.hasMoreElements()) {
							ZipEntry entry = (ZipEntry) entries.nextElement();
							String nowClassName = getClassName(entry);
							String inputClassName = this.className
									.toUpperCase();
							String thisClassName = nowClassName.toUpperCase();

							if ((thisClassName.equals(inputClassName))
									|| (thisClassName.equals(inputClassName
											+ ".CLASS"))
									|| (thisClassName.contains(inputClassName))
									|| (thisClassName.contains(inputClassName
											+ ".CLASS"))) {
								String[] rets = new String[3];
								rets[0] = subfilename;
								rets[1] = nowClassName;
								rets[2] = filename;
								this.jarFiles.add(rets);
							}
						}
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List getFilenames() {
		return this.jarFiles;
	}

	protected String getClassName(ZipEntry entry) {
		StringBuffer className = new StringBuffer(entry.getName().replace("/",
				"."));
		return className.toString();
	}

	public static List startSearch(String className, String searchPath) {
		String tclassName = className;
		String findPath = searchPath;
		Object[][] reto = (Object[][]) null;
		FindInJar findInJar = new FindInJar(tclassName);
		List jarFiles = findInJar.findClass(findPath, true);
		if (jarFiles.size() == 0)
			System.out.println("Not Found");
		else {
			for (int i = 0; i < jarFiles.size(); i++) {
				System.out.println(jarFiles.get(i));
			}
		}
		return jarFiles;
	}

	public static void main(String[] args) {
		if (args.length < 2) {
			System.out.println("must input className and findPath");
			return;
		}
		String tclassName = args[0];
		String findPath = args[1];
		System.out.println("start find " + tclassName + " at " + findPath);

		FindInJar findInJar = new FindInJar(tclassName);
		List jarFiles = findInJar.findClass(findPath, true);
		if (jarFiles.size() == 0) {
			System.out.println("Not Found");
		} else {
			for (int i = 0; i < jarFiles.size(); i++)
				System.out.println(jarFiles.get(i));
		}
	}
}
